package com.zx._12_算法.字节跳动面试题;

import com.zx._12_算法.TreeNode;
import java.util.LinkedList;

/**
 * 每一层，只打印左边的第一个值
 */
public class 二叉树的左视图 {

    public static void main(String[] args) {
        TreeNode root = TreeNode.root;
        LinkedList<TreeNode> list = new LinkedList<TreeNode>();
        list.add(root);

        while (list.size() != 0) {
            String str = "";//只打印每一层第一个
            int len = list.size();
            for (int i = 0; i < len; i++) {
                TreeNode pop = list.pop();
                // 只打印第一个值
                if (i == 0) {
                    str += pop.val;
                }
                if (pop.left != null) {
                    list.add(pop.left);
                }
                if (pop.right != null) {
                    list.add(pop.right);
                }
            }
            System.out.println(str);
        }
    }

}
